﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

using XRGLib;
using Console = XRGLib.Console;

namespace Examples
{
    abstract class ExampleBase
    {
        protected Console console;
        protected Surface surface;

        protected Rand rand;

        public abstract string  Description{get;}

        protected class Cell : IFovCell, IPathCell<object>
        {
            public Cell(bool isWall, int x, int y)
            {
                this.IsWall = isWall;
                visible = false;

                this.X = x;
                this.Y = y;
            }

            bool visible;

            public int X
            {
                get;
                private set;
            }

            public int Y
            {
                get;
                private set;
            }

            public bool IsWall
            {
                get;
                set;
            }

            public bool IsTransparent
            {
                get
                {
                    if (IsWall)
                        return false;
                    else
                        return true;
                }
            }

            public bool IsVisible
            {
                get
                {
                    return visible;
                }
                set
                {
                    visible = value;
                }
            }

            public bool IsWalkable(object context)
            {
                if (IsWall)
                    return false;
                else
                    return true;
            }
        }

        protected Array2d<Cell> map;

        protected Game game;

        public ExampleBase(Console console, int width, int height, Game game)
        {
            this.console = console;
            this.game = game;

            rand = new Rand();

            surface = console.CreateSurface(width, height);

            map = new Array2d<Cell>(width, height);
            for (int y = 0; y < map.Height; y++)
                for (int x = 0; x < map.Width; x++)
                    map[x, y] = new Cell(false, x, y);

            MakeMap();
        }

        public virtual Surface ReDraw()
        {
            return surface;
        }

        public virtual void OnKeyboard(KeyEventArgs e)
        {
        }

        public virtual void OnMouseButton(MouseButton button, int cx, int cy)
        {
        }

        public virtual void Update(GameTime gameTime)
        {
        }

        public virtual void OnActivated()
        {
        }

        public virtual void OnDeactivated()
        {
        }

        protected void MakeMap()
        {


            BSPTree bsp = new BSPTree(surface.Rect);

            bsp.SplitRecursive(2, 8, 8, rand);

            foreach (var n in bsp.GetAllLeaves())
            {
                int wallChance = rand.GetInt(0, 30);
                for (int y = n.Rect.Top; y < n.Rect.Bottom; y++)
                {
                    for (int x = n.Rect.Left; x < n.Rect.Right; x++)
                    {
                        if (rand.GetInt(0, 99) <= wallChance)
                            map[x, y].IsWall = true;
                        else
                            map[x, y].IsWall = false;
                    }
                }
            }
        }
    }
}
